package com.eugeneMa.controller;

import com.eugeneMa.entity.Bell;
import com.eugeneMa.entity.Result;
import com.eugeneMa.enumEntity.ErrorMsg;
import com.eugeneMa.service.inter.BellService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * 音频控制层，提供获取音频等接口
 *
 * @author Eugenema
 * @date 2022/4/6 19:42
 */
@Controller
@RequestMapping("/bell")
public class BellController {
    /** 日志 */
    private Logger logger = Logger.getLogger(BellController.class);

    /** 音频service */
    @Autowired
    BellService bellService;

    /**
     * 获取指定音频文件，以流形式返回至前端
     *
     * @author Eugenema
     * @date 2022/4/7 16:03
     *
     * @param id 音频文件id
     *
     * @return 封装结果集，若正常获取，会返回前端音频流。反之会有出错信息。
     **/
    @RequestMapping("/getBell/{id}")
    @ResponseBody
    public Result getBell(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取用户id，后期会获取指定用户的音频
            Integer userId = (Integer)request.getAttribute("userId");

            //获取音频文件
            Result result = bellService.getBellById(id);

            //返回音频流
            if (result.getFlag()) {
                Bell bell = (Bell)result.getData();
                String filePath = request.getSession().getServletContext().getRealPath("/") + bell.getFilePath();

                //音频文件
                File audio = new File(filePath);
                if (audio.exists()) {
                    OutputStream os = response.getOutputStream();
                    FileInputStream fis = new FileInputStream(audio);
                    FileCopyUtils.copy(fis, os);
                } else {
                    return new Result(false, null, ErrorMsg.FILE_NO_EXISTS);
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取指定音频文件【Id：" + id + "】异常！", e);
            return new Result(false, null, ErrorMsg.SYSTEM_ERROR);
        }
    }

    /**
     * 获取所有音频文件
     *
     * @author Eugenema
     * @date 2022/4/7 16:06
     *
     * @return 所有音频文件对象
     **/
    @RequestMapping("/getBell")
    @ResponseBody
    public Result getBell(HttpServletRequest request) {
        try {
            //获取用户id，后期会获取指定用户的音频
            Integer userId = (Integer)request.getAttribute("userId");

            //获取所有音频文件
            return bellService.getBell();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取所有音频文件异常！", e);
            return new Result(false, null, ErrorMsg.SYSTEM_ERROR);
        }
    }
}
